package com.zbzly.reactive.reactiverbac.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import lombok.Data;

import java.util.Date;
import java.util.Map;

@Data
public class JwtUtil {
    private String secret;
    private Long expiration;
    private String issuer;
    private static final String SECRET = "qwertyuiopasdfghjklzxcvbnm1234567890";

    public JwtUtil(String secret, Long expiration, String issuer) {
        this.secret = secret;
        this.expiration = expiration;
        this.issuer = issuer;
    }

    public String generateToken(String subject) {
        return generateToken(subject, null);
    }

    public String generateToken(String subject, Map<? extends String, ?> claims) {
        return Jwts.builder()
                .signWith(Keys.hmacShaKeyFor(secret.getBytes()))
                .subject(subject)
                .header()
                .and()
                .claims()
                .add(claims)
                .issuer("https://www.zbzly.top")
                .issuedAt(new Date())
                .expiration(new Date(new Date().getTime() + 1000L * 60 * 60 * 24 * expiration))
                .and()
                .compact();
    }

    public Claims parseToken(String token) {
        return Jwts.parser()
                .verifyWith(Keys.hmacShaKeyFor(secret.getBytes()))
                .build()
                .parseSignedClaims(token)
                .getPayload();
    }
}
